package com.example.administrator.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {
     EditText mEUserName;
     TextView mTvMsg;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mEUserName = findViewById(R.id.et_username);
        mTvMsg = findViewById(R.id.tv_msg);
    }

        public void login(View view){
            String username=mEUserName.getText().toString();
         try {
             FileOutputStream fileOutputStream=openFileOutput("1.txt",MODE_PRIVATE);

             byte[] msg = username.getBytes();
             fileOutputStream.write(msg);
             fileOutputStream.close();
         } catch (FileNotFoundException e) {
             e.printStackTrace();
         } catch (IOException e) {
             e.printStackTrace();
         }
        }


    public void read(View view) {
        try {
            FileInputStream fileInputStream
                    = openFileInput("1.txt");
            byte[] bytes = new byte[fileInputStream.available()];
            fileInputStream.read(bytes);
            String mag = new String(bytes);

            mTvMsg.setText(mag);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}

